<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            /* 切割字符串s，让其符合IP地址条件，所以也是用回溯，需要记录start，终止条件自然是start==s.length */
            var restoreIpAddresses = function (s) {
                let res = []
                function backTracking(path, startIndex) {
                    //剪枝  path==['211','11','2','34']长度是4！！！ temp才是目标元素
                    if (path.length > 4) return
                    if (startIndex == s.length && path.length == 4) {
                        return res.push([...path].join('.'))
                    }
                    for (let i = startIndex; i < startIndex + 3; i++) {
                        let temp = s.slice(startIndex, i + 1)
                        if (Number(temp) < 0 || Number(temp) > 255) {
                            continue
                        }
                        if (temp.length > 1 && temp[0] == '0') {
                            continue
                        }
                        path.push(temp)
                        backTracking(path, i + 1)
                        path.pop()
                    }
                }
                backTracking([], 0)
                return res
            }

            console.log(restoreIpAddresses('25525511135'))
        </script>
    </body>
</html>
